package com.abo.yaas.view.widgets.panels;

import com.abo.yaas.view.AbstractView;
import com.abo.yaas.view.widgets.LocaleSensitiveButton;
import com.abo.yaas.view.widgets.tables.AuctionTable;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;

@SuppressWarnings("serial")
public class AuctionTablePanel extends AbstractView<Panel> {
    private final VerticalLayout layout;

    private final Panel panel;

    private final AuctionTable table = new AuctionTable();

    private final HorizontalLayout searchLayout = new HorizontalLayout();

    private final TextField searchField = new TextField();

    private final Button searchButton = new LocaleSensitiveButton("SEARCH_BUTTON");

    public AuctionTablePanel() {
        super(new Panel());
        panel = getContent();
        layout = new VerticalLayout();
        layout.addStyleName("auctions");
        layout.setWidth("100%");
        layout.setHeight("100%");
        layout.setMargin(false);

        final ComponentContainer controls = table.createControls();
        searchButton.addStyleName(Runo.BUTTON_SMALL);
        searchButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                table.filter(String.valueOf(searchField.getValue()));
            }
        });
        searchLayout.setWidth("100%");
        searchLayout.setSpacing(true);
        searchLayout.setMargin(true);
        searchField.setWidth("100%");

        searchLayout.addComponent(searchButton);
        searchLayout.addComponent(searchField);
        searchLayout.addComponent(controls);
        searchLayout.setExpandRatio(searchField, 1.0f);

        layout.addComponent(searchLayout);
        layout.addComponent(table);
        layout.setExpandRatio(table, 1f);

        panel.setContent(layout);
        panel.setSizeFull();
    }

    @Override
    public void activated(Object... params) {
        table.refreshCurrentPage();
    }

    @Override
    public void deactivated(Object... params) {

    }
}
